package com.example.common.res;


import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.Date;

/**
 * 统一返回数据
 * 特殊编码 5001 响应客户端，但是不用被提示框弹出
 */
@Data
@Accessors(chain = true)
public class R implements Serializable {
    private static final long serialVersionUID = 1L;

    private int code;
    private String msg;
    private Object data;
    private Long time;


    public R() {
        this.code = 200;
        this.msg = "success";
        this.time = new Date().getTime();
    }

    public R(int code, String msg) {
        this.code = code;
        this.msg = msg;
        this.time = new Date().getTime();
    }

    public R(Object data) {
        this.code = 200;
        this.msg = "success";
        this.data = data;
        this.time = new Date().getTime();
    }

    public R(Object data, String msg) {
        this.code = 200;
        this.msg = msg;
        this.data = data;
        this.time = new Date().getTime();
    }

    public static R error() {
        return error(500, "error");
    }

    public static R error(String msg) {
        return error(700, msg);
    }


    public static R error(int code, String msg) {
        return new R(code, msg);
    }

    public static R ok(Object data, String msg) {
        return new R(data, msg);
    }

    public static R ok(Object data) {
        return new R(data);
    }

    public static R ok() {
        return new R();
    }




    public String toJsonString() {
        return JSONObject.toJSONString(this);
    }

}